/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gestorxml;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author asc
 */
public class GestorXML {

    public static void otra(String... aArgs) throws IOException {
        GestorAPP.main(aArgs);
    }

    private static String debug;
    
    public static String[][] listado(String destination) throws Exception {
        
        debug = "";
        XMLWriter.initialize();
        XMLWriter.CreateRootElement("root");
        XMLWriter.AppendChild("counter", 0, "root");
        XMLWriter.AppendChild("folders", 0, "root");
        XMLWriter.AppendChild("files", 0, "root");

        
        FileProcesor fileProcessor = new FileProcesor(destination);
        Files.walkFileTree(Paths.get(fileProcessor.getROOT()), fileProcessor);

        XMLWriter.SetAttribute("counter0", "f", String.valueOf(fileProcessor.getFiles()));
        XMLWriter.SetAttribute("counter0", "d", String.valueOf(fileProcessor.getDirs()));
        String[] fileName = destination.split("/");
        String nombre = fileName[fileName.length-1] + ".xml";
        XMLWriter.Finalize(nombre);
        Validator.validateXML(nombre, "DTD");
        
        
       debug = fileProcessor.getDirectoryList() + "\n" + fileProcessor.getFileList();
       HashMap<String,String[]> map =fileProcessor.getRows();
       return getData(map);
    }

    public static String[][] xmlRead(String file, String filter)
    {
        HashMap<String,String[]> map = new HashMap<String, String[]>();
        String salida="";
        if(Validator.validateXML(file, "master.dtd"))
        {
            XMLReader.initialize(file);
            NodeList dirs = XMLReader.BuscaElemento("folder");
            NodeList files = XMLReader.BuscaElemento("file");

            for(int tmp=0; tmp<dirs.getLength(); tmp++)
            {
                Node nNode = dirs.item(tmp);
                Element eElement = (Element) nNode;
                
                String id = XMLReader.getAtributeValue("id", eElement);
                String father =  XMLReader.getAtributeValue("father", eElement);//PADRE
                String name =    XMLReader.getTagValue("name", eElement);//NOMBRE
                String path =    XMLReader.getTagValue("path", eElement);//PATH
                //String size =    XMLReader.getTagValue("size", eElement);//TAM
                //String type =    XMLReader.getTagValue("type", eElement);//TIPO
                Node xdirs = XMLReader.getNode("dir", eElement);
                String dir =     XMLReader.getAtributeValue("x", xdirs);
                Node xfiles = XMLReader.getNode("fi", eElement);
                String fi =     XMLReader.getAtributeValue("x", xfiles);
                
                String[] a = {
                    id,     //ID,
                    father, //PADRE
                    name,   //NOMBRE
                    path,   //PATH
                    dir,     //DIR
                    fi,     //FI
                    "",   //TAM
                    ""    //TIPO
                };
                if(name.contains(filter))
                {
                    map.put(id, a);
                }
                salida+=XMLReader.getTagValue("path", eElement);
                salida+= "\n";
            }

            for(int tmp=0; tmp<files.getLength(); tmp++)
            {
                Node nNode = files.item(tmp);
                Element eElement = (Element) nNode;
                
                String id = XMLReader.getAtributeValue("id", eElement);
                String father =  XMLReader.getAtributeValue("father", eElement);//PADRE
                String name =    XMLReader.getTagValue("name", eElement);//NOMBRE
                String path =    XMLReader.getTagValue("path", eElement);//PATH
                String size =    XMLReader.getTagValue("size", eElement);//TAM
                String type =    XMLReader.getTagValue("type", eElement);//TIPO
                //Node xdirs = XMLReader.getNode("dir", eElement);
                //String dir =     XMLReader.getAtributeValue("x", xdirs);
                //Node xfiles = XMLReader.getNode("fi", eElement);
                //String fi =     XMLReader.getAtributeValue("x", xfiles);
                
                String[] a = {
                    id,     //ID,
                    father, //PADRE
                    name,   //NOMBRE
                    path,   //PATH
                    "",     //DIR
                    "",     //FI
                    size,   //TAM
                    type    //TIPO
                };
                if(name.contains(filter))
                {
                    map.put(id, a);
                }
                
                salida+=XMLReader.getTagValue("path", eElement);
                salida+= "\n";
            }
        }
        else
        {
            System.out.println("ERROR");
        }
        debug = salida;
        return  getData(map);
    }

    private static String[][] getData(HashMap<String,String[]> map)
    {
       String[][] x = new String[map.size()][8];
       int i=0;
        for (String[] strings : map.values()) {
            x[i]=strings;
            i++;     
        }
        return x;
    }
    
    
    public static boolean validate(String namefile)
    {
        return Validator.validateXML(namefile,"master.dtd");
    }

    public static String getDebug() {
        return debug;
    }
}
